/**
 * @filename:SysknowledgeBaseController 2025-05-09
 * @project starhub-schema  V1.0
 */
package com.starhub.domain.knowledge.controller;

import com.starhub.application.collection.entity.KnowledgeCollection;
import com.starhub.application.collection.service.KnowledgeCollectionService;
import com.starhub.common.bean.ID;
import com.starhub.common.bean.ResultResp;
import com.starhub.common.security.util.SecurityUtils;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.domain.knowledge.dto.KnowledgeCollectionDto;
import com.starhub.domain.knowledge.service.KnowledgeSettingService;
import com.starhub.utils.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>说明： 知识库API接口层</P>
 * @version: V1.0
 * @author: luosw
 * @time    2025-06-20
 *
 */
@Api(value="向量知识库设置后台接口", tags = {"向量知识库设置后台接口"},description = "编写者:luosw" )
@RestController
@RequestMapping("/engine/starhub/knowledge/collection")
public class KnowledgeCollectionConfigController {

    @Autowired
    private KnowledgeCollectionService knowledgeCollectionService;

    @Autowired
    private KnowledgeSettingService knowledgeSettingService;


    /**
    * 日志
    */
    private static final Logger log = LoggerFactory.getLogger(KnowledgeCollectionConfigController.class);

    /**
    * 向量知识库设置新增
    */
    @ApiOperation(value = "向量知识库设置新增", notes = "向量知识库设置新增")
    @PostMapping("/add")
    public ResultResp add(@RequestBody KnowledgeCollection knowledgeCollection){
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            knowledgeCollection.setCreatorId(Util.getLongValue(user.getUserId()));
            knowledgeCollection.setCreateTime(LocalDateTime.now());
            KnowledgeCollectionDto KnowledgeCollectionDto = new KnowledgeCollectionDto();
            BeanUtils.copyProperties(knowledgeCollection, KnowledgeCollectionDto);
            boolean result = knowledgeSettingService.createKnowledgeCollection(KnowledgeCollectionDto);
            return ResultResp.success(result);
        } catch (Exception e) {
            log.error("创建知识库表失败", e);
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
    * 向量知识库设置修改
    */
    @ApiOperation(value = "向量知识库设置修改", notes = "向量知识库设置修改")
    @PostMapping("/update")
    public ResultResp update(@RequestBody KnowledgeCollection knowledgeCollection){
        try {
            knowledgeCollection.setUpdateTime(LocalDateTime.now());
            KnowledgeCollectionDto KnowledgeCollectionDto = new KnowledgeCollectionDto();
            BeanUtils.copyProperties(knowledgeCollection, KnowledgeCollectionDto);
            boolean result = knowledgeSettingService.updateKnowledgeCollection(KnowledgeCollectionDto);
            return ResultResp.success(result);
        } catch (Exception e) {
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
    * 向量知识库设置信息获取
    */
    @ApiOperation(value = "向量知识库设置信息获取", notes = "向量知识库设置信息获取")
    @GetMapping("/getInfo/{id}")
    public ResultResp getInfo(@PathVariable Long id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            ResultResp = ResultResp.success(knowledgeCollectionService.selectById(id));
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
    * 向量知识库设置逻辑删除
    */
    @ApiOperation(value = "向量知识库设置逻辑删除", notes = "向量知识库设置逻辑删除")
    @PostMapping("/delete")
    public ResultResp deleteByType(@RequestBody ID id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            knowledgeCollectionService.deleteById(Util.getLongValue(id.getId()));
            ResultResp = ResultResp.success("删除成功");
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }


    /**
    * 向量知识库设置物理删除
    */
    @ApiOperation(value = "向量知识库设置物理删除", notes = "向量知识库设置物理删除")
    @PostMapping("/deleteRecord")
    public ResultResp deleteRecord(@RequestBody ID id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            knowledgeCollectionService.deleteRecord(Util.getLongValue(id.getId()));
            ResultResp = ResultResp.success("删除成功");
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
    * 向量知识库设置列表获取
    */
    @ApiOperation(value = "向量知识库设置列表获取", notes = "向量知识库设置列表获取")
    @GetMapping("/getList")
    public ResultResp getList(HttpServletRequest request){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            Map<String , Object> params = Util.request2Map(request);
            ResultResp = ResultResp.success(knowledgeCollectionService.selectByMap(params));
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }


    @ApiOperation(value = "知识库列表获取", notes = "知识库列表获取")
    @GetMapping("/getActiveKnowledgeCollection")
    public ResultResp getActiveKnowledgeCollection(HttpServletRequest request){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            Map<String , Object> params = Util.request2Map(request);
            ResultResp = ResultResp.success(knowledgeCollectionService.getActiveKnowledgeCollection(params));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

}